//The MIT License
//
//Copyright (c) <2011> <GEPAR - Grupo de Ensino e Pesquisa em Agroinformática e Rastreabilidade>
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:
//
//The above copyright notice and this permission notice shall be included in
//all copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//THE SOFTWARE.

package gui;

import core.BancoDadosLocal;
import java.awt.BorderLayout;
import java.awt.SplashScreen;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

public class FramePrincipal extends javax.swing.JFrame {

    public FramePrincipal()
    {
        this.initComponents();
        
        // botões, quando tiverem o foco, passam a ser disparados com o pressionar de ENTER
        UIManager.getDefaults().put("Button.defaultButtonFollowsFocus", Boolean.TRUE);

        this.setExtendedState(MAXIMIZED_BOTH);

        // FramePrincipal terá layout tipo Border Layout
        // tabPrincipal ocupará a posição CENTER do layout
        BorderLayout bl = new BorderLayout();
        bl.addLayoutComponent(this.scrollpanePrincipal, BorderLayout.CENTER);
        this.setLayout(bl);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrollpanePrincipal = new javax.swing.JScrollPane();
        tabPrincipal = new javax.swing.JTabbedPane();
        menubTopo = new javax.swing.JMenuBar();
        menuArquivo = new javax.swing.JMenu();
        mitemNovaTabul = new javax.swing.JMenuItem();
        mitemEnviarTabul = new javax.swing.JMenuItem();
        mitemSair = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Hermes - O tabulador mais rápido do Olimpo!");

        scrollpanePrincipal.setViewportView(tabPrincipal);

        menuArquivo.setText("Arquivo");

        mitemNovaTabul.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.ALT_MASK));
        mitemNovaTabul.setText("Nova tabulação");
        mitemNovaTabul.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mitemNovaTabulActionPerformed(evt);
            }
        });
        menuArquivo.add(mitemNovaTabul);

        mitemEnviarTabul.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.ALT_MASK));
        mitemEnviarTabul.setText("Enviar tabulações para o servidor");
        mitemEnviarTabul.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mitemEnviarTabulActionPerformed(evt);
            }
        });
        menuArquivo.add(mitemEnviarTabul);

        mitemSair.setText("Sair");
        mitemSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mitemSairActionPerformed(evt);
            }
        });
        menuArquivo.add(mitemSair);

        menubTopo.add(menuArquivo);

        setJMenuBar(menubTopo);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrollpanePrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(216, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scrollpanePrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(139, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mitemNovaTabulActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mitemNovaTabulActionPerformed

        PanelConfigNovaTabul panelConfigNTabul = new PanelConfigNovaTabul(tabPrincipal);
        this.tabPrincipal.addTab("Configuração de Nova Tabulação", panelConfigNTabul);
    }//GEN-LAST:event_mitemNovaTabulActionPerformed

    private void mitemSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mitemSairActionPerformed

        int confirmacao;
        confirmacao = JOptionPane.showConfirmDialog(this, "Caso haja alguma tabulação em andamento,"
                + " seus dados serão perdidos.Você tem certeza que deseja fechar o Hermes?",
                "Fechar o Hermes?",
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);

        if (confirmacao == JOptionPane.OK_OPTION)
        {
            this.dispose();
        }
    }//GEN-LAST:event_mitemSairActionPerformed

    private void mitemEnviarTabulActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mitemEnviarTabulActionPerformed
        // TODO add your handling code here:

        PanelEnviaTabulacoes panelEnviaTabul = new PanelEnviaTabulacoes(this.tabPrincipal, this.mitemEnviarTabul);
        this.tabPrincipal.addTab("Enviar Tabulações para o Servidor", panelEnviaTabul);

    }//GEN-LAST:event_mitemEnviarTabulActionPerformed

    public static void main(String args[])
    {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                
                // criação (se necessário) e inicialização do banco de dados embarcado (Java DB)
                BancoDadosLocal bancoDados = BancoDadosLocal.getInstancia();

                try
                {
                    bancoDados.inicializar();

                    SplashScreen splashHermes = SplashScreen.getSplashScreen();

                    if (splashHermes != null)
                    {
                        // o carregamento já foi concluído; fechamos a SplashScreen
                        splashHermes.close();
                    }

                    new FramePrincipal().setVisible(true);

                } catch (Exception e)
                {
                    JOptionPane.showMessageDialog(null, e.getMessage());
                }
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu menuArquivo;
    private javax.swing.JMenuBar menubTopo;
    private javax.swing.JMenuItem mitemEnviarTabul;
    private javax.swing.JMenuItem mitemNovaTabul;
    private javax.swing.JMenuItem mitemSair;
    private javax.swing.JScrollPane scrollpanePrincipal;
    private javax.swing.JTabbedPane tabPrincipal;
    // End of variables declaration//GEN-END:variables

}
